from django.db import models

'''
  带有外键的对象字典会返回外键_id的属性，值是对应的id 
  当查到的数据是model对象实体时外键存储的是user_name(例子)
'''
# Create your models here.

# 获取当前时区的时间
from django.utils import timezone


# 用户表
class UserInfo(models.Model):
    collect_news = models.ManyToManyField('news.NewsInfo', verbose_name='收藏的资讯', related_name='news_collect_id',
                                          blank=True, db_index=True)
    collect_activities = models.ManyToManyField('activity.ActivityInfo', verbose_name='收藏的活动',
                                                related_name='collect_id',
                                                blank=True, db_index=True)
    activities = models.ManyToManyField('activity.ActivityInfo', verbose_name='参与的活动', related_name='joined_id',
                                        blank=True, db_index=True)
    user_name = models.CharField(verbose_name='用户名', max_length=15, unique=True, db_index=True)
    phone = models.CharField(verbose_name='手机号码', max_length=11, unique=True, db_index=True)
    password = models.CharField(verbose_name='密码', max_length=50)
    real_name = models.CharField(verbose_name='真实姓名', max_length=15, default='')
    nick_name = models.CharField(verbose_name='昵称', max_length=11, default='一位光荣的志愿者')
    sex = models.CharField(verbose_name='性别', max_length=2, default='未知')
    age = models.IntegerField(verbose_name='年龄', default=18)
    email = models.EmailField(verbose_name='联系邮箱', default='')
    avatar = models.FileField(verbose_name='用户头像', upload_to='userAvatar',
                              default='userAvatar/2021-11-16_165658.175493__13991044970.jpg')
    user_number = models.CharField(verbose_name='证件号码', max_length=21, default='')
    user_nation = models.CharField(verbose_name='民族', max_length=15, default='汉族')
    user_status = models.CharField(verbose_name='政治面貌', max_length=10, default='群众')
    user_school = models.CharField(verbose_name='学校', max_length=15, default='西安理工大学')
    user_school_id = models.CharField(verbose_name='学号或职工号', max_length=15, default='')
    qq_number = models.CharField(verbose_name='QQ号码', max_length=11, default='')
    user_occupation = models.CharField(verbose_name='从业情况', max_length=15, default='')
    user_edu_background = models.CharField(verbose_name='最高学历', max_length=10, default='')
    user_intro = models.TextField(verbose_name='个人简介', max_length=300, default='请介绍一下你自己吧')
    create_time = models.DateTimeField(verbose_name='创建时间', default=timezone.now)
    is_management = models.BooleanField(verbose_name='管理权限', max_length=5, default=False)

    class Meta:
        db_table = 'userinfo'
        verbose_name = "用户表"
        verbose_name_plural = verbose_name

    def __str__(self):
        """将模型类以字符串的方式输出"""
        return self.user_name


# 留言表
class LeaveWordInfo(models.Model):
    leave_word_content = models.CharField(verbose_name='留言内容', max_length=150, default='')
    sender = models.ForeignKey(UserInfo, verbose_name='留言者', related_name='leave_word_id', on_delete=models.SET_NULL,
                               null=True, db_index=True)
    leave_word_create_time = models.DateTimeField(verbose_name='创建时间', default=timezone.now)

    class Meta:
        db_table = 'leavewordinfo'
        verbose_name = "留言表"
        verbose_name_plural = verbose_name
